*******************************
* Table A11
*******************************
clear
use masterdata_county.dta

encode state, gen(state2)
gen senior = (pop_55above)/pop
gen whiteratio = white/pop
gen blackratio = black/pop
gen a1 = white/pop
gen a2 = black/pop
gen a3 = indian/pop
gen a4 = asian/pop
gen a5 = pacific/pop
gen a6 = otherace/pop
gen a7 = tworaces/pop
gen a8 = hispanic/pop
gen ethnic = 1 - (a1^2 + a2^2 + a3^2 + a4^2 + a5^2 + a6^2 + a7^2 +a8^2)
gen nohs = lessthanhs/pop_over25
gen hs = highschool/pop_over25 
gen loweredu = nohs + hs
gen college = bachelor/pop_over25
gen advanced = (master + professional + doctorate)/pop_over25
gen unemployment = civilian_unemployed/civilian
gen male_unemployment = male_umemployed/civilian_male
gen female_unemployment = female_umemployed/civilian_female
gen white_unemployment = whilte_unemployed/whilte_laborforce
gen publicsectorratio = public_sector/employed_civilian
gen manufacturingratio = manufacturing/employed_civilian
gen agricultureratio = agriculture/employed_civilian
gen foreignratio = foreign_born/pop
gen noinsuranceratio = noinsurance/insurance_pop

gen lpop = ln(pop +1)
gen lnpcincome = ln(pcincome+1)

gen repratio08 = gop_2008/(dem_2008 + gop_2008)
gen repratio12 = gop_2012/(dem_2012 + gop_2012)
gen repratio16 = gop_2016/(dem_2016 + gop_2016)

macro define demographic "lpop senior whiteratio loweredu lnpcincome unemployment white_unemployment manufacturingratio d_tradeusch_pw foreignratio noinsuranceratio"

gen ratio1 = certifyingtaa1/tottaa1
gen ratio2 = certifyingtaa2/tottaa2
gen ratio3 = certifyingtaa3/tottaa3

replace ratio1 = 0 if ratio1 ==.
replace ratio2 = 0 if ratio2 ==.
replace ratio3 = 0 if ratio3 ==.

gen affected1 = ln(estimated1 +1)
gen affected2 = ln(estimated2 +1)
gen affected3 = ln(estimated3 +1)

gen repchange1 = (repratio16 - repratio08)*100
gen repchange2 = (repratio16 - repratio12)*100

macro define taa1 "tottaa1 ratio1 tottaa2 ratio2 tottaa3 ratio3"
macro define taa2 "tottaa1 affected1 tottaa2 affected2 tottaa3 affected3"

eststo clear
eststo: quietly areg rep_primary_trump_vs $taa1 $demographic, a(state2) vce(cluster state2)
eststo: quietly areg rep_primary_trump_vs $taa2 $demographic, a(state2) vce(cluster state2)
eststo: quietly areg repchange1 $taa1 $demographic, a(state2) vce(cluster state2)
eststo: quietly areg repchange1 $taa2 $demographic, a(state2) vce(cluster state2)
eststo: quietly areg repchange2 $taa1 $demographic, a(state2) vce(cluster state2)
eststo: quietly areg repchange2 $taa2 $demographic, a(state2) vce(cluster state2)
esttab, star(* 0.1 ** 0.05 *** 0.01) ar2
